package com.cskaoyan.service;

import com.cskaoyan.bean.RegionListBean;
import com.cskaoyan.bean.RegionProvince2VO;
import com.cskaoyan.bean.RegionProvince3VO;
import com.cskaoyan.bean.RegionProvinceVO;
import com.cskaoyan.mapper.RegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * @USER: cuiweiguo#
 * @DATE: 2022/5/6
 * @TIME: 22:48
 * @PROJECT_NAME: mallproject2
 */
@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    RegionMapper regionMapper;
    @Override
    public RegionListBean list() {
        //"data":{"total":31,"pages":1,"limit":31,"page":1,
        //查询总共所有的数量
        //查询所有的页面
        int total = regionMapper.countAllByPid();
        int pages  = 1;
        int limit = total;
        int page =1;
        List<RegionProvinceVO> list=  regionMapper.queryAll();
        Iterator<RegionProvinceVO> iterator = list.iterator();
        while(iterator.hasNext()){
            RegionProvinceVO regionProvinceVO = iterator.next();
            Integer provinceVOCode = regionProvinceVO.getCode();
            int code = provinceVOCode;
            //System.out.println(province2VOCode);
            List<RegionProvince2VO> list2 = regionMapper.selectByProvince2VOCode(code);
            Iterator<RegionProvince2VO> iterator1 = list2.iterator();
            while(iterator1.hasNext()){
                RegionProvince2VO province2VO = iterator1.next();
                Integer province3VOCode = province2VO.getCode();
                int code2  = province3VOCode;
                List<RegionProvince3VO> list3 = regionMapper.selectByProvince3VO(code2);
                province2VO.setChildren(list3);
            }
            regionProvinceVO.setChildren(list2);
        }
        return RegionListBean.data(total, pages, limit, page, list);

        //return null;
    }
}
